﻿using HBase;
using HBase.cache;
using HBase.util;
using hCommand;
using hCommand.entity;
using hCommand.mySql;
using log4net.Config; 
using System;
using System.Data.SqlClient;

namespace hCommand {
    [RoutePrefix("api/jnjp")]
    public class ChouyangServe : BaseApiController {
        IMServer mServer = GuidangFactray.getDal<IMServer>();
        IChouYangServer chouYangServer = WMSDalFactray.getDal<IChouYangServer>();
        [HttpGet, Route("cj/{code}")]
        public HeadlessBaseResult cj(string code) {
            if (String.IsNullOrEmpty(code))
            {
                LoggerCommon.consol("param emply");
                return HeadlessBaseResult.ok("ok");
            }
           
            if (code.StartsWith("A")) {
                return A(code.Substring(1));
            }
            return HeadlessBaseResult.ok("#"); 
        }
        HeadlessBaseResult A(string b64) {//A版本
            b64 = b64.Replace('>', '/');//自定义base64
            byte[] p = Convert.FromBase64String(b64);
            if (CRC.checkCRC16(p)) {
                return HeadlessBaseResult.ok("CRC");
            }

            byte[] idArr = new byte[] { p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0] };
            long id = BitConverter.ToInt64(p, 0);
            String code = HUtil.to64(id);
            CyEqup e = getCyEqup( code);
            if (e == null) {
                return HeadlessBaseResult.ok("E");
            }
            String domain = getDomain(e);
            EqupPos pos = Protocol_v_A.analysis(p, code);
            pos.n = e.custom;
            var n = pos.time;
            String name = CyUtil.getName(n);
            mServer.save(domain, pos, name);

            byte[] data = Protocol_v_A.packet(p, e);
            var d = Convert.ToBase64String(data, Base64FormattingOptions.None);
            return HeadlessBaseResult.ok(d);
        }

        CyEqup getCyEqup(String code) {
            CyEqup e = MyCache.getValue<CyEqup>("CyEqup", code);
            if (e == null) { 
                e=new CyEqup();  
                e.code = code;
                e.canWork = 1;
                e.alias = code;
                e.useEndTime = DateTime.Now;
                e.id = WMSIdUtil.getId();
               
                chouYangServer.insert(BaseConfig.BaseDBConnStr, e);
              
                
                TaskPool.doWork<WcsLoadUp>();
                e = MyCache.getValue<CyEqup>("CyEqup", code);
            }
            return e;
        }


        String getDomain(CyEqup e) {

            int status = 0;
            string domain = BaseConfig.BaseDBConnStr;
            long useId = 0;
            CyUser u = WcsLoadUp.getEqupUser(e);
            if (u != null) {
                useId = u.id;
                string suffix = u.suffix;
                if (!string.IsNullOrEmpty(suffix)) {
                    string[] suffixArr = suffix.Split("@");
                    if (suffixArr.Length == 2) {
                        domain = suffixArr[1];

                    } else {
                        status = 4;
                    }
                } else {
                    status = 3;
                }
            } else {
                status = 2;
            }

            return domain;
        } 
    }

   public class Protocol_v_A {
        public static   byte[] packet(byte[] p, CyEqup e) {

            byte[] data = new byte[23];
            for (int i = 0; i < 8; i++) {
                data[i] = p[i];
            }
            data[8] = (byte)e.volCoil1;
            data[9] = (byte)e.volCoil2;
            data[10] = (byte)e.coilHz;
            data[11] = (byte)e.tmpFan;
            data[12] = (byte)e.tmpLimitCeil;
            data[13] = (byte)e.tmpLimitFloor;
            data[14] = (byte)e.vLimitCeil;
            data[15] = (byte)e.vLimitFloor;

            data[16] = (byte)e.iLimitCeil;
            data[17] = (byte)e.iLimitFloor;
            data[18] = (byte)e.nextQTime;
            data[19] = (byte)e.offlineTime;
            data[20] = (byte)e.canWork;
            CRC.setCRC16(data);
            return data;
        }

        public static EqupPos analysis(byte[] p,   String code) {
            EqupPos pos = new EqupPos();
            pos.code = code;
            String v = (p[8] / 10.0 + 10 + "");
            pos.v1 = v;
            pos.hz =p[9]==0?0:p[9]/10.0+40;
            pos.i = p[10]/10.0;
            pos.tmp = p[11]-100;
            pos.tc = p[12];
            pos.tl = p[13];
            pos.ta = p[14];
            pos.bm = p[15];
            pos.s = p[18];
            pos.e = p[19];
            pos.w = p[20];
            return pos;
        }

    }
}
